﻿var ajaxsetting = {
    template : "<div class='progressbar'>数据加载中，请稍等...</div>"
};
var ajaxManager = function (app) {
    this.application = app;
    this.send = function (option) {
        option = $.extend({
            icon: alertsetting.icon.info,
            text: "数据加载中，请稍等...",
            id: "ajax_" + new Date().getTime(),
            maskid: "mask_ajax_" + new Date().getTime(),
            mask: true,
            zindex: null,
            maskzindex: null,
            parent: "body",
            content: null,
            application: this.application,
            onsuccess: function (data, obj) { },
            aftersuccess: function (jo) { },
            onerror: function () { },
            type: "json"//josn,text
        }, option);
        if (option.maskzindex == null) {
            option.maskzindex = this.application.getZindexManager().getTop(option.maskid);
        }
        if (option.zindex == null) {
            option.zindex = this.application.getZindexManager().getTop(option.id);
        }
        var ajax = $(ajaxsetting.template);
        var helper = new $.ajaxHelper(ajax, option);
        ajax.helper("ajax", helper);
        helper.init();
        return helper;
    };
    this.ajaxpost = function (option) {
        option = $.extend({
            url: null,
            data: null,
            successmsg: "操作成功",
            tabid: false,
            fill: false,
            type: "json",
            backtype: "alert", //alert,fill,refresh
            onsuccess: function (data, obj) {
                if (data) {
                    if (data.indexOf("请填写用户名") > 0 && data.indexOf("请填写验证码") > 0) {
                        top.location.href = "/";
                        return;
                    }
                }

                if (obj.setting.backtype == "alert") {
                    obj.setting.application.getAlertManager().alert(obj.setting.successmsg);
                    if (obj.setting.aftersuccess) {
                        obj.setting.aftersuccess(data);
                    }
                    return;
                }
                if (!obj.setting.successmsg) {
                    if (obj.setting.backtype == "refresh" && obj.setting.tabid) {

                        obj.setting.application.getTabPanelContainer().refreshTab(obj.setting.tabid);
                        return;
                    }
                    if (obj.setting.backtype == "fill") {
                        if (!obj.setting.fill) {
                            var fillobj = $("[panelid='" + obj.setting.tabid + "']");
                            fillobj.html(data);
                            fillobj.renderui();
                            obj.setting.aftersuccess(fillobj);
                        }
                        else {
                            var fillobj = $(obj.setting.fill);
                            if (obj.setting.tabid) {
                                fillobj = $(obj.setting.fill, $("[panelid='" + obj.setting.tabid + "']"));
                            };
                            fillobj.html(data);
                            fillobj.renderui();
                            obj.setting.aftersuccess(fillobj);
                        }
                    }
                } else {
                    obj.setting.application.getAlertManager().alert(obj.setting.successmsg, function () {

                        if (obj.setting.backtype == "refresh" && obj.setting.tabid) {
                            obj.setting.application.getTabPanelContainer().refreshTab(obj.setting.tabid);
                            return;
                        }
                        if (obj.setting.backtype == "fill") {
                            if (!obj.setting.fill) {
                                var fillobj = $("[panelid='" + obj.setting.tabid + "']");
                                fillobj.html(data);
                                fillobj.renderui();
                                obj.setting.aftersuccess(fillobj);
                            }
                            else {
                                var fillobj = $(obj.setting.fill);
                                if (obj.setting.tabid) {
                                    fillobj = $(obj.setting.fill, $("[panelid='" + obj.setting.tabid + "']"));
                                };
                                fillobj.html(data);
                                fillobj.renderui();
                                obj.setting.aftersuccess(fillobj);
                            }
                        }
                    });
                }
            },
            onerror: function (data) {
                this.application.getAlertManager().error(data);
            }
        }, option);
        option.content = option.url;
        if (option.data) {
            option.content = { url: option.url, postdata: option.data }
        }
        this.send(option);
    };

};


(function ($) {
    $.ajaxHelper = function (jo, setting) {
        this.jo = jo;
        this.setting = setting;
        this.content = null;
        this.mask = null;
        var _this = this;
        this.init = function () {
            this.jo.attr("ajaxid", this.setting.id);
            this.jo.attr("id", this.setting.id);
            this.jo.find(".progressbar").html(this.setting.text);
            this.jo.css("z-index", this.setting.zindex);
            this.content = this.jo.find(".msg");
            
            if (this.setting.mask) {
                this.mask = $("<div class=\"mask\"></div>");
                this.mask.attr("id", this.setting.maskid);
                this.mask.attr("maskid", this.setting.maskid);
                this.mask.css("z-index", this.setting.maskzindex);
                $(this.setting.parent).append(this.mask);
            }
            $(this.setting.parent).append(this.jo);
            this.sendrequest();
        };
        this.sendrequest = function () {
            var reqobj={};
            if (typeof _this.setting.content == "string" || !_this.setting.content.postdata) {
                reqobj.url=_this.setting.content;
                reqobj.type="GET";
            }else{
                reqobj.url=_this.setting.content.url;
                reqobj.data=_this.setting.content.postdata;
                reqobj.type="POST";
            }
            if(_this.setting.type=="html"){
                reqobj.dataType="html";                
            }else if(_this.setting.type=="json"){
                reqobj.dataType="json";
            }
            reqobj.url=reqobj.url.nocache();
            reqobj.success=function(data){
                if(_this.setting.type=="html"){
                    _this.setting.onsuccess(data,_this);
                }else if(_this.setting.type=="json"&&data.success){                
                    _this.setting.onsuccess(data.result,_this);
                }else{
                    var errmsg="服务器错误";
                    if(data&&data.errmsg){
                        errmsg=data.errmsg;
                    }
                    _this.setting.onerror(errmsg,_this);                    
                }
                _this.close();
            };
            reqobj.error=function(e){
                _this.setting.onerror("服务器错误",_this);
                _this.close();
            };
            $.ajax(reqobj);
        };
        
        this.close = function () {
            this.setting.application.getZindexManager().remove(this.setting.id);
            this.jo.remove();
            this.mask && this.mask.remove();

//            var height=this.jo.height();
//            this.jo.animate({top:height*-1+"px"},500,function(){                
//                _this.setting.application.getZindexManager().remove(_this.setting.id);
//                _this.jo.remove();
//                _this.mask && _this.mask.remove();
//            });
        };
    };
})(jQuery);